package com.mjw.tender.controller;

import com.mjw.tender.api.ArticleService;
import com.mjw.tender.domain.Article;
import com.mjw.tender.dto.FavouriteArticleDTO;
import com.mjw.tender.dto.ResponseResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
import java.util.List;


@RestController
@RequestMapping("articles")
public class ArticleController {

    @Resource
    RedisTemplate redisTemplate;

    @Reference(version = "1.0.0")
    ArticleService articleService;


    @PostMapping
    public ResponseResult add(@RequestBody Article article){
        Long id = articleService.add(article);
        return new ResponseResult(ResponseResult.CodeStatus.OK,id);
    }

    @GetMapping("/")
    public ResponseResult<List<Article>>  getArticleCard(@RequestParam("currPage") Integer currPage,
                                                            @RequestParam("pageSize") Integer pageSize,
                                                            @RequestParam("categoryId") Long categoryId){
        Article article = new Article();
        if(categoryId >0){
            article.setClassId(categoryId);
        }
        Integer offset = (currPage - 1)*pageSize;
        List list = articleService.selectListByArticle(article,currPage,pageSize);
        return new ResponseResult(ResponseResult.CodeStatus.OK,list);
    }

    /**
     * 阅读全文
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public ResponseResult readArticle(@PathVariable("id") Long id){
        Article article = articleService.read(id); //获取文章内容
        return new ResponseResult(ResponseResult.CodeStatus.OK,article);
    }

    /**
     *
     */
    @GetMapping(value = "/favourites",params = {"userId"})
    public ResponseResult<FavouriteArticleDTO> getFavouriteArticle(@RequestParam("userId")Long userId){
        List<Article> likeList = articleService.getLikeArticle(userId);
        List<Article> collectList = articleService.getCollectArticle(userId);
        FavouriteArticleDTO favouriteArticleDTO = new FavouriteArticleDTO();
        favouriteArticleDTO.setCollectList(collectList);
        favouriteArticleDTO.setLikeList(likeList);
        return new ResponseResult(ResponseResult.CodeStatus.OK,favouriteArticleDTO);
    }

    /**
     * 获取浏览次数最高的文章
     * @param num
     * @return
     */
    @GetMapping("/hot")
    public ResponseResult<List<Article>> getHotBrowseList(@RequestParam("num")Integer num){
        List<Article> list = articleService.getHotBrowseList(num);
        return new ResponseResult(ResponseResult.CodeStatus.OK,list);
    }

    /**
     * 1.从缓存中获取浏览数，点赞数，收藏数
     * 2.设置到article中
     * @param article
     */
    private void setArticleFavourite(Article article){
        String aricleId = article.getId().toString();
        String browse_key = "BROWSE_COUNT-" + aricleId;
        String like_key = "LIKE_COUNT-" + aricleId;
        String collect_key = "COLLECT_COUNT-" +aricleId;
        Long browseCount = (Long) redisTemplate.opsForValue().get(browse_key);
        Long likeCount = (Long) redisTemplate.opsForValue().get(like_key);
        Long collectCount = (Long) redisTemplate.opsForValue().get(collect_key);
        // TODO 分布式锁
        if(browseCount == null){
            redisTemplate.opsForValue().set(browse_key,article.getBrowseCount());
        }else {
            browseCount =redisTemplate.opsForValue().increment(browse_key); // 阅读量+1
            article.setBrowseCount(browseCount);
        }
        if (like_key == null) {
            redisTemplate.opsForValue().set(like_key,article.getLikeCount());
        }else {
            article.setCollectCount(likeCount);
        }
        if(like_key == null){
            redisTemplate.opsForValue().set(collect_key,article.getCollectCount());
        }else{
            article.setCollectCount(collectCount);
        }
    }
}
